package com.erp.dao.impl;

import com.erp.bean.Evaluate;
import com.erp.dao.EvaluateDao;
import com.erp.util.db.DataBaseDao;

import java.sql.Date;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * @author mtf
 * 评价数据访问实现类
 */
public class EvaluateDaoImpl extends DataBaseDao implements EvaluateDao {
    @Override
    public List<Evaluate> findAll() {
        List<Evaluate> list = new ArrayList<>();
        try {
            //获取数据库连接对象
            buildConnection();
            //创建sql语句
            String sql = "select * from evaluate";
            //创建预编译指令
            pstmt = conn.prepareStatement(sql);
            //执行预编译指令
            rs = pstmt.executeQuery();
            while (rs.next()){
                Evaluate evaluate =new Evaluate(rs.getInt(1),rs.getInt(2),rs.getString(3),rs.getString(4),rs.getTimestamp(5));
                list.add(evaluate);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭
            close();
        }


        return list;
    }

    @Override
    public Evaluate findByEvaId(int evaid) {
        Evaluate evaluate = null;
        try {
            //获取数据库连接对象
            buildConnection();
            //创建sql语句
            String sql = "select * from evaluate where evaid = ?";
            //创建预编译对象
            pstmt = conn.prepareStatement(sql);
            //设置参数
            pstmt.setInt(1,evaid);
            //执行预编译指令
            rs = pstmt.executeQuery();
            while (rs.next()){
                evaluate = new Evaluate(rs.getInt(1),rs.getInt(2),rs.getString(3),rs.getString(4),
                        rs.getTimestamp(5));

            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            //关闭
            close();
        }
        return evaluate;
    }

    @Override
    public int add(Evaluate evaluate) {
        int num = 0;
        try {
            //获取数据库连接对象
            buildConnection();
            //创建sql语句
            String sql = "insert into evaluate (supid,oid,content,evatime) values (?,?,?,?)";
            //创建预编译对象
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,evaluate.getSupid());
            pstmt.setString(2,evaluate.getOid());
            pstmt.setString(3,evaluate.getContent());
            pstmt.setTimestamp(4,evaluate.getEvatime());


                num = pstmt.executeUpdate();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭
            close();
        }
        return num;
    }

    @Override
    public int update(Evaluate evaluate) {
        int num = 0;
        try {
            //获取数据库连接对象
            buildConnection();
            //创建sql语句
            String sql = "update evaluate set supid = ?, oid = ? ,content = ?, evatime = ? where evaid = ?";
            //创建预编译对象
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,evaluate.getSupid());
            pstmt.setString(2,evaluate.getOid());
            pstmt.setString(3,evaluate.getContent());
            pstmt.setTimestamp(4,evaluate.getEvatime());
            pstmt.setInt(5,evaluate.getEvaid());
            num = pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭
            close();
        }
        return num;
    }

    @Override
    public List<Evaluate> findBySupId(int supid) {
        List<Evaluate> list = new ArrayList<>();
        try {
            //获取数据库连接对象
            buildConnection();
            //创建sql语句
            String sql = "select * from evaluate where supid = (?)";
            //创建预编译对象
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,supid);
            rs = pstmt.executeQuery();
            while (rs.next()){
                Evaluate evaluate = new Evaluate(rs.getInt(1),rs.getInt(2),rs.getString(3),rs.getString(4),rs.getTimestamp(5));
                list.add(evaluate);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭
            close();
        }
        return list;
    }

    @Override
    public List<Evaluate> find(String column, String keywords) {
        List<Evaluate> list = new ArrayList<>();
        try {
            //获取数据库连接对象
            buildConnection();
            //创建sql语句
            String sql = "select * from evaluate where " + column + " like ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,"%"+keywords+"%");
            rs = pstmt.executeQuery();
            while (rs.next()){
                Evaluate evaluate = new Evaluate(rs.getInt(1),rs.getInt(2),rs.getString(3),rs.getString(4),rs.getTimestamp(5));
                list.add(evaluate);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭
            close();
        }
        return list;
    }

    @Override
    public Evaluate findByOId(String oid) {
        Evaluate evaluate = null;
        try {
            //获取数据库连接对象
            buildConnection();
            //创建sql语句
            String sql = "select * from evaluate where oid = ?";
            //创建预编译对象
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,oid);
            //执行预编译指令
            rs = pstmt.executeQuery();
            while (rs.next()){
                evaluate = new Evaluate(rs.getInt(1),rs.getInt(2),rs.getString(3),rs.getString(4),
                        rs.getTimestamp(5));

            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
        }
        return evaluate;
    }
}
